﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using EMANIFEST.DataAccess.Models;
using EMANIFEST.Dtos;


namespace EMANIFEST.DataAccess
{
    public class Dto2Xml
    {

        public static  string DangerousGood2XML(DangerousGoodDto dg)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xtx = XmlWriter.Create(sb);
            xtx.WriteStartDocument();

            xtx.WriteStartElement("","DangerousGoods","");

            xtx.WriteStartElement("", "DangerousGood", "");
            
            xtx.WriteAttributeString("PortOfLoad", dg.PortOfLoad.ToString());

            xtx.WriteAttributeString("PortOUnload", dg.PortOfUnload.ToString());

            xtx.WriteAttributeString("ContNumber", dg.ContNumber);

            xtx.WriteAttributeString("ContSealNumber", dg.ContSealNumber);

            xtx.WriteAttributeString("ItemStatus", dg.ItemStatus.GetValueOrDefault(0).ToString());

            if(!string.IsNullOrEmpty(dg.PortNameOfLoad.ToString()))
                xtx.WriteAttributeString("PortNameOfLoad", dg.PortNameOfLoad);

            if (!string.IsNullOrEmpty(dg.PortNameOfUnload.ToString()))
                xtx.WriteAttributeString("PortNameOfUnload", dg.PortNameOfUnload);
            
            xtx.WriteStartElement("", "DangerousGoodItems", "");
            foreach (var item in dg.DangerousGoodItems)
            {
                xtx.WriteStartElement("", "DangerousGoodItem", "");

                xtx.WriteAttributeString("DangerousGoodItemId", item.DangerousGoodItemId.ToString());

                xtx.WriteAttributeString("RefNumber", item.RefNumber);

                if(!string.IsNullOrEmpty(item.NumberContainer))
                     xtx.WriteAttributeString("NumberContainer", item.NumberContainer);

                if (!string.IsNullOrEmpty(item.NumberOfPackage))
                   xtx.WriteAttributeString("NumberOfPackage", item.NumberOfPackage);

                if (!string.IsNullOrEmpty(item.ProperShipping))
                      xtx.WriteAttributeString("ProperShipping", item.ProperShipping);

                if (!string.IsNullOrEmpty(item.GoodClass))
                    xtx.WriteAttributeString("GoodClass", item.GoodClass);

                if (!string.IsNullOrEmpty(item.UNNumber))
                    xtx.WriteAttributeString("UNNumber", item.UNNumber);

                if (!string.IsNullOrEmpty(item.PackingGroup))
                    xtx.WriteAttributeString("PackingGroup", item.PackingGroup);

                if (!string.IsNullOrEmpty(item.SubsidiaryRisk))
                    xtx.WriteAttributeString("SubsidiaryRisk", item.SubsidiaryRisk);

                xtx.WriteAttributeString("FlashPoint", item.FlashPoint.GetValueOrDefault().ToString());

                if (!string.IsNullOrEmpty(item.MarinePollutant))
                    xtx.WriteAttributeString("MarinePollutant", item.MarinePollutant);

                xtx.WriteAttributeString("GrossWeight", item.GrossWeight.GetValueOrDefault().ToString());

                if(!string.IsNullOrEmpty(item.StowagePosition))
                    xtx.WriteAttributeString("StowagePosition", item.StowagePosition);

                xtx.WriteAttributeString("ItemStatus", item.ItemStatus.GetValueOrDefault(0).ToString());

                xtx.WriteEndElement();
            }
            xtx.WriteEndElement();  // end of DangerousGoodItems
            xtx.WriteEndElement();  // end of DangerousGood
            xtx.WriteEndElement();  // end of DangerousGoods
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



        public static string HouseBill2XML(HouseBillDto hb)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xtx = XmlWriter.Create(sb);
            xtx.WriteStartDocument();

            xtx.WriteStartElement("", "HouseBills", "");

            xtx.WriteStartElement("", "HouseBill", "");
            
            xtx.WriteAttributeString("Consigner", hb.Consigner);

            xtx.WriteAttributeString("Consignee", hb.Consignee);

            xtx.WriteAttributeString("PortOfTranship", hb.PortIdOfTranship.ToString());

            xtx.WriteAttributeString("PortOfDestination", hb.PortIdOfDestination.ToString());

            xtx.WriteAttributeString("PortOfLoad", hb.PortIdOfLoad.ToString());

            xtx.WriteAttributeString("PortOfUnload", hb.PortIdOfUnload.ToString());

            xtx.WriteAttributeString("BillNumber", hb.BillNumber);

            xtx.WriteAttributeString("DateOfBill", hb.DateOfBill.ToString("dd/MM/yyyy"));

            xtx.WriteAttributeString("DepartureDate", hb.DepartureDate.ToString("dd/MM/yyyy"));

            xtx.WriteAttributeString("ItemStatus", hb.ItemStatus.ToString());


            if(!string.IsNullOrEmpty(hb.MasterBillNo))
                xtx.WriteAttributeString("MasterBillNo", hb.MasterBillNo);

            xtx.WriteAttributeString("DateOfMasterBill", hb.DateOfMasterBill.GetValueOrDefault().ToString("dd/MM/yyyy"));

            xtx.WriteAttributeString("NumberOfPackage", hb.NumberOfPackage.GetValueOrDefault(0).ToString());

            if(!string.IsNullOrEmpty(hb.KindOfPackages))
                xtx.WriteAttributeString("KindOfPackages", hb.KindOfPackages);

            if (!string.IsNullOrEmpty(hb.PortNameOfTranship))
                xtx.WriteAttributeString("PortNameOfTranship", hb.PortNameOfTranship);

            if (!string.IsNullOrEmpty(hb.PortNameOfDestination))
                xtx.WriteAttributeString("PortNameOfDestination", hb.PortNameOfDestination);

            if (!string.IsNullOrEmpty(hb.PortNameOfLoad))
                xtx.WriteAttributeString("PortNameOfLoad", hb.PortNameOfLoad);

            if (!string.IsNullOrEmpty(hb.PortNameOfUnload))
                xtx.WriteAttributeString("PortNameOfUnload", hb.PortNameOfUnload);
            

            xtx.WriteStartElement("", "HouseBillItems", "");
            foreach (var item in hb.HouseBillItems)
            {
                xtx.WriteStartElement("", "HouseBillItem", "");
                xtx.WriteAttributeString("HouseBillItemId", item.HouseBillItemId.ToString());

                if(!string.IsNullOrEmpty(item.Description))
                    xtx.WriteAttributeString("Description", item.Description);

                xtx.WriteAttributeString("GrossWeight", item.GrossWeight.ToString());

                xtx.WriteAttributeString("Demension", item.Demension.ToString());

                xtx.WriteAttributeString("ContNumber", item.ContNumber);

                xtx.WriteAttributeString("ContSealNumber", item.ContSealNumber);

                xtx.WriteAttributeString("ItemStatus", item.ItemStatus.GetValueOrDefault().ToString());

                xtx.WriteEndElement();
            }
            xtx.WriteEndElement();  // end of HouseBillItems
            xtx.WriteEndElement();  // end of HouseBill
            xtx.WriteEndElement();  // end of HouseBills
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



        public static string GoodDeclaration2XML(GoodDeclarationDto objGoodDeclarationDto)
        {
            StringBuilder xmlString = new StringBuilder();

            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = Encoding.Unicode;
            XmlWriter xtx = XmlWriter.Create(xmlString, setting);

            xtx.WriteStartDocument();

            xtx.WriteStartElement("", "GoodDeclarations", "");

            xtx.WriteStartElement("", "GoodDeclaration", "");

            xtx.WriteAttributeString("Decription", objGoodDeclarationDto.Decription);

            if (objGoodDeclarationDto.NumberOfPackage.HasValue)
                xtx.WriteAttributeString("NumberOfPackage", objGoodDeclarationDto.NumberOfPackage.Value.ToString());

            if (!string.IsNullOrEmpty(objGoodDeclarationDto.KindOfPackages))
                xtx.WriteAttributeString("KindOfPackages", objGoodDeclarationDto.KindOfPackages);

            if (objGoodDeclarationDto.ItemStatus.HasValue)
                xtx.WriteAttributeString("ItemStatus", objGoodDeclarationDto.ItemStatus.Value.ToString());

            if (objGoodDeclarationDto.ModifyId.HasValue)
                xtx.WriteAttributeString("ModifyId", objGoodDeclarationDto.ModifyId.Value.ToString());

            xtx.WriteAttributeString("SortOrder", objGoodDeclarationDto.SortOrder.ToString());

            xtx.WriteStartElement("", "GoodItems", "");
            foreach (var item in objGoodDeclarationDto.GoodItems)
            {
                xtx.WriteStartElement("", "GoodItem", "");

                if (item.GoodItemId != null)
                    xtx.WriteAttributeString("GoodItemId", item.GoodItemId.ToString());

                if (!string.IsNullOrEmpty(item.MasterBillNo))
                    xtx.WriteAttributeString("MasterBillNo", item.MasterBillNo);

                if (!string.IsNullOrEmpty(item.Consigner))
                    xtx.WriteAttributeString("Consigner", item.Consigner);

                if (!string.IsNullOrEmpty(item.Consignee))
                    xtx.WriteAttributeString("Consignee", item.Consignee);

                if (!string.IsNullOrEmpty(item.NotificatedParty))
                    xtx.WriteAttributeString("NotificatedParty", item.NotificatedParty);

                if (!string.IsNullOrEmpty(item.ContNumber))
                    xtx.WriteAttributeString("ContNumber", item.ContNumber);

                if (!string.IsNullOrEmpty(item.ContSealNumber))
                    xtx.WriteAttributeString("ContSealNumber", item.ContSealNumber);

                if (!string.IsNullOrEmpty(item.GoodDescription))
                    xtx.WriteAttributeString("GoodDescription", item.GoodDescription);

                if (item.NetWeight.HasValue)
                    xtx.WriteAttributeString("NetWeight", item.NetWeight.Value.ToString());

                if (item.GrossWeight.HasValue)
                    xtx.WriteAttributeString("GrossWeight", item.GrossWeight.Value.ToString());

                if (item.Demension.HasValue)
                    xtx.WriteAttributeString("Demension", item.Demension.Value.ToString());

                xtx.WriteAttributeString("RefNumber", item.RefNumber);

                if (!string.IsNullOrEmpty(item.Ajustment))
                    xtx.WriteAttributeString("Ajustment", item.Ajustment);

                if (item.ItemStatus.HasValue)
                    xtx.WriteAttributeString("ItemStatus", item.ItemStatus.Value.ToString());

                xtx.WriteEndElement();  //end GoodItem
            }
            xtx.WriteEndElement();  //end GoodItems
            xtx.WriteEndElement();  //end GoodDeclaration
            xtx.WriteEndElement();  //end GoodDeclarations


            xtx.WriteEndDocument(); 

            xtx.Flush();
            xtx.Close();

            return xmlString.ToString();
        }



        public static  string ShipStoreDeclaration2XML(ShipStoreDeclarationDto sd)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xtx = XmlWriter.Create(sb);
            xtx.WriteStartDocument();

            xtx.WriteStartElement("", "ShipStoreDeclarations", "");

            xtx.WriteStartElement("", "ShipStoreDeclaration", "");

            xtx.WriteAttributeString("PeriodOfStay", sd.PeriodOfStay.ToString("dd/MM/yyyy"));

            xtx.WriteAttributeString("PlaceOfStorage", sd.PlaceOfStorage.ToString());

            xtx.WriteAttributeString("ItemStatus", sd.ItemStatus.GetValueOrDefault(0).ToString());

            xtx.WriteAttributeString("ModifyId", sd.ModifyId.GetValueOrDefault().ToString());
            
            xtx.WriteStartElement("", "ShipStoreItems", "");
            foreach (var item in sd.ShipStoreItems)
            {
                xtx.WriteStartElement("", "ShipStoreItem", "");
                xtx.WriteAttributeString("ShipStoreItemId", item.ShipStoreItemId.ToString());

                xtx.WriteAttributeString("Name", item.Name);

                xtx.WriteAttributeString("Quantity", item.Quantity.ToString());

                xtx.WriteAttributeString("ItemStatus", item.ItemStatus.GetValueOrDefault().ToString());

                xtx.WriteEndElement();
            }
            xtx.WriteEndElement();  // end of ShipStoreItems
            xtx.WriteEndElement();  // end of ShipStoreDeclaration
            xtx.WriteEndElement();  // end of ShipStoreDeclarations
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



        public static string Crew2XML(List<CrewDto> listCrewDto)
        {
            StringBuilder sb = new StringBuilder();

            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = Encoding.Unicode;
            XmlWriter xtx = XmlWriter.Create(sb, setting);

            xtx.WriteStartDocument();
            xtx.WriteStartElement("", "Crews", "");

            foreach (var item in listCrewDto)
            {
                xtx.WriteStartElement("", "Crew", "");

                xtx.WriteAttributeString("CrewId", item.CrewId.ToString());

                if (!string.IsNullOrEmpty(item.FamilyName))
                    xtx.WriteAttributeString("FamilyName", item.FamilyName);

                if (!string.IsNullOrEmpty(item.GivenName))
                    xtx.WriteAttributeString("GivenName", item.GivenName);

                if (!string.IsNullOrEmpty(item.Rank))
                    xtx.WriteAttributeString("Rank", item.Rank);

                xtx.WriteAttributeString("StateId", item.StateId.ToString());

                xtx.WriteAttributeString("BirthDate", item.BirthDate.ToString("dd/MM/yyyy"));

                if (!string.IsNullOrEmpty(item.BirthPlace))
                    xtx.WriteAttributeString("BirthPlace", item.BirthPlace);

                if (!string.IsNullOrEmpty(item.PassportType))
                    xtx.WriteAttributeString("PassportType", item.PassportType);

                if (!string.IsNullOrEmpty(item.PassportNumber))
                    xtx.WriteAttributeString("PassportNumber", item.PassportNumber);

                xtx.WriteAttributeString("PassportValidDate", item.PassportValidDate.ToString("dd/MM/yyyy"));

                if (item.ItemStatus.HasValue)
                    xtx.WriteAttributeString("ItemStatus", item.ItemStatus.Value.ToString());

                if (item.ModifyId != null)
                    xtx.WriteAttributeString("ModifyId", item.ModifyId.Value.ToString());


                xtx.WriteEndElement();  // end of Crew
            }
            xtx.WriteEndElement();  // end of Crews
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



        public static string CrewEffect2XML(List<CrewEffectDeclarationDto> ced)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xtx = XmlWriter.Create(sb);
            xtx.WriteStartDocument();

            xtx.WriteStartElement("", "CrewEffectDeclarations", "");

            foreach (var item in ced)
            {
                xtx.WriteStartElement("", "CrewEffectDeclaration", "");

                xtx.WriteAttributeString("CrewEffectDeclarationId", item.CrewEffectDeclarationId.ToString());

                if (!string.IsNullOrEmpty(item.DutyPackage.ToString()))
                    xtx.WriteAttributeString("DutyPackage", item.DutyPackage);

                if (!string.IsNullOrEmpty(item.FullName))
                    xtx.WriteAttributeString("FullName", item.FullName);

                if (!string.IsNullOrEmpty(item.Rank))
                    xtx.WriteAttributeString("Rank", item.Rank);

                if(item.ModifyId.HasValue)
                    xtx.WriteAttributeString("ModifyId", item.ModifyId.Value.ToString());

                xtx.WriteEndElement();  // end of CrewEffectDeclarations
            }

            xtx.WriteEndElement();  // end of CrewEffectDeclaration
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



        public static  string Passenger2XML(List<PassengerDto> p)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.Unicode;

            XmlWriter xtx = XmlWriter.Create(sb, settings);
            xtx.WriteStartDocument();

            xtx.WriteStartElement("", "Passengers", "");

            foreach (var item in p)
            {
                xtx.WriteStartElement("", "Passenger", "");

                xtx.WriteAttributeString("PassengerId", item.PassengerId.ToString());

                xtx.WriteAttributeString("FamilyName", item.FamilyName);

                if (!string.IsNullOrEmpty(item.GivenName))
                    xtx.WriteAttributeString("GivenName", item.GivenName);

                xtx.WriteAttributeString("StateId", item.StateId.ToString());

                xtx.WriteAttributeString("BirthDate", item.BirthDate.ToString("dd/MM/yyyy"));

                xtx.WriteAttributeString("BirthPlace", item.BirthPlace);

                xtx.WriteAttributeString("PassportType", item.PassportType);

                xtx.WriteAttributeString("PassportNumber", item.PassportNumber);

                xtx.WriteAttributeString("PassportValidDate", item.PassportValidDate.ToString("dd/MM/yyyy"));

                xtx.WriteAttributeString("PortIdOfEmbarkation", item.PortIdOfEmbarkation.ToString());

                xtx.WriteAttributeString("PortOfDisembarkation", item.PortIdOfDisembarkation.ToString());

                xtx.WriteAttributeString("IsTransit", item.IsTransit.ToString());

                xtx.WriteAttributeString("ItemStatus", item.ItemStatus.GetValueOrDefault(0).ToString());

                if (item.PortId != null)
                    xtx.WriteAttributeString("PortId", item.PortId.Value.ToString());

                if (item.ModifyId != null)
                    xtx.WriteAttributeString("ModifyId", item.ModifyId.Value.ToString());

                if (!string.IsNullOrEmpty(item.PortNameOfEmbarkation))
                    xtx.WriteAttributeString("PortNameOfEmbarkation", item.PortNameOfEmbarkation);

                if (!string.IsNullOrEmpty(item.PortNameOfDisembarkation))
                    xtx.WriteAttributeString("PortNameOfDisembarkation", item.PortNameOfDisembarkation);

                xtx.WriteEndElement();  // end of Passenger
            }
            
            xtx.WriteEndElement();  // end of Passengers
            xtx.Flush();
            xtx.Close();
            return sb.ToString();
        }



    }
}